package com.chen.lock;

import java.util.concurrent.locks.ReentrantLock;

// lock版可重入锁
// lock 锁必须配对，否则就会死在里面
public class ReentrantLockTest2 {
    public static void main(String[] args) {

        Phone2 phone = new Phone2();

        new Thread(phone::sms, "A").start();
        new Thread(phone::sms, "B").start();

    }
}

class Phone2 {

    ReentrantLock lock = new ReentrantLock();

    // 一个锁里面有另一个锁
    public void sms() {
        lock.lock();
        try {
            System.out.println(Thread.currentThread().getName() + "发短信");
            // 这里有一个锁
            call();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    private void call() {
        lock.lock();
        try {
            System.out.println(Thread.currentThread().getName() + "打电话");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

}